package com.example.pilipili.controller;


import com.example.pilipili.common.R;
import com.example.pilipili.pojo.dto.VInfo;
import com.example.pilipili.pojo.entity.Video;
import com.example.pilipili.service.CollectionService;
import com.example.pilipili.service.FollowService;
import com.example.pilipili.service.LikeService;
import com.example.pilipili.service.VideoService;
import com.example.pilipili.utils.FileU;
import jakarta.validation.Valid;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyv
 * @since 2023-12-01
 */
@RestController
@RequestMapping("/videos")
public class VideoController {
    @Autowired
    VideoService videoService ;


    /**
     * 上传视频
     * @param uid 用户id
     * @param img 视频封面
     * @param video 视频
     * @param title 标题
     * @param tid 分类id
     * @param status 状态id
     * @return videos
     */
    @PostMapping
    public R<Video> saveVideo(
            @RequestParam("uid") int uid ,
            @RequestParam("img") MultipartFile img ,
            @RequestParam("video")MultipartFile video ,
            @RequestParam("title")String title,
            @RequestParam("tid")int tid,
            @RequestParam("status")int status
    ) {
        Video v = new Video();
        v.setTime(LocalDate.now());
        v.setUid(uid);
        v.setTid(tid);
        v.setTitle(title);
        v.setStatus(status);
        v.setIUrl(FileU.uploadFileToLocal(img,"covers"));
        v.setVUrl(FileU.uploadFileToLocal(video,"video"));

        videoService.save(v) ;
        return R.ok(v);
    }

    /**
     * 视频更改
     * @param img 封面
     * @param video 视频
     * @param title 标题
     * @param tid 分类
     * @param vid 视频id
     * @param status 状态
     * @return video
     */
    @PostMapping("/update")
    public R<Video> updateVideo(
            @RequestParam(name = "img" , required = false) MultipartFile img ,
            @RequestParam(name = "video", required = false)MultipartFile video ,
            @RequestParam("title")  String title,
            @RequestParam("tid")int tid,
            @RequestParam("vid")int vid,
            @RequestParam("status")int status
    ) {
        Video v = new Video();
        v.setId(vid);
        v.setTid(tid);
        v.setTitle(title);
        v.setStatus(status);
        if(img != null) { // 更新封面
            v.setIUrl(FileU.uploadFileToLocal(img,"covers"));
        }
        if(video!= null) { // 更新视频
            v.setVUrl(FileU.uploadFileToLocal(video,"video"));
        }

        return videoService.update(v) ;
    }

    /**
     * 根据 tid 分页查询
     * @param data tid,page,size
     * @return list video
     */
    @PostMapping("/t")
    public R<List<Video>> getByTag(@RequestBody Map<String,Integer> data) {
        return videoService.selectByT(data.get("tid"),data.get("page"),data.get("size"));
    }

    /**
     * 根据id获取视频信息
     * @param vid vid
     * @return VInfo
     */
    @GetMapping("/detail/{vid}")
    public R<VInfo> getInfoByVid(@PathVariable int vid) {
        return videoService.getVInfoByVid(vid) ;
    }

    /**
     * 获取某用户的点赞列表
     * @param uid uid
     * @return list video
     */
    @GetMapping("/likeu")
    public R<List<Video>> getsByULove(@RequestParam("u") int uid) {
        return videoService.getsByULove(uid) ;
    }

    /**
     * 获取某用户的收藏列表
     * @param uid uid
     * @return list video
     */
    @GetMapping("/cu")
    public R<List<Video>> getsByUCollection(@RequestParam("u") int uid) {
        return videoService.getsByUCollection(uid) ;
    }

    /**
     * 根据 tid title 模糊查询
     * @param data tid,page,size
     * @return list video
     */
    @PostMapping("/liket")
    public R<List<Video>> getByTagLike(@RequestBody Map<String,String> data) {
        return videoService.selectByLike(
                Integer.parseInt(data.get("tid")),
                Integer.parseInt(data.get("page")),
                Integer.parseInt(data.get("size")),
                data.get("likes"));
    }

    /**
     * 更改status
     * @param data vid,newStatus,note
     * @return  video
     */
    @PostMapping("/s")
    public R<Video> changeStatus(@RequestBody  Map<String,String> data ) {
        return videoService.changeStatus(
                Integer.parseInt(data.get("vid")),
                Integer.parseInt(data.get("newStatus")),
                data.get("note")
        ) ;
    }

    /**
     * 根据 status 和 用户 获取列表
     * @param status status
     * @param uid uid
     * @return list video
     */
    @GetMapping("/su")
    public R<List<Video>> getBySU(@RequestParam("s") int status ,@RequestParam("u") int uid) {

        return videoService.getsByUidSid(uid,status) ;
    }

    /**
     * 点击+1
     * @param vid vid
     * @return video
     */
    @GetMapping("/click/{vid}")
    public R<Video> addClick(@PathVariable int vid){
        return videoService.addClick(vid) ;
    }


    @DeleteMapping("/{vid}")
    public R del(@PathVariable int vid) {
        videoService.removeById(vid) ;
        return R.ok();
    }

    /**
     * 获取等待审核的视频信息
     * @return list vinfo
     */
    @GetMapping("/waits")
    public R<List<VInfo>> getsWaiting() {
        return videoService.getsWaitForReview() ;
    }

    /**
     * 审核结果
     * @param v id,new status
     * @return video
     */
    @PostMapping("/audit")
    public R<Video> videoAudit(@RequestBody @Valid Video v){
        return videoService.changeStatus(v);
    }




}

